package com.hymjweb.demo.hymj.customer_manage.invoice.service;

import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.invoice.bean.BCustomerInvoicePojo;
import com.hymjweb.demo.hymj.customer_manage.invoice.mapper.BCustomerInvoiceMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BCustomerInvoiceService extends BaseService {


    @Autowired
    private BCustomerInvoiceMapper bCustomerInvoiceMapper;

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="客户发票抬头-添加" , operMemo = "", classType =  BCustomerInvoicePojo.class)
    @Transactional
    public int insert(BCustomerInvoicePojo pojo) throws Exception {
        if(pojo==null){
            throwException("客户发票抬头添加参数错误!");
        }
        return bCustomerInvoiceMapper.insert(pojo);
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="客户发票抬头-更新" , operMemo = "", classType =  BCustomerInvoicePojo.class)
    @Transactional
    public int update(BCustomerInvoicePojo pojo) throws Exception {
        if(pojo==null){
            throwException("客户发票抬头更新参数错误!");
        }
        return bCustomerInvoiceMapper.update(pojo);
    }

    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="客户发票抬头-删除" , operMemo = "", classType =  BCustomerInvoicePojo.class)
    @Transactional
    public int delete(String invoiceId) throws Exception {
        if(DataTypeUtil.isEmptyStr(invoiceId)){
            throwException("客户发票抬头删除参数错误!");
        }
        return bCustomerInvoiceMapper.delete(invoiceId);
    }


    @Transactional
    public int batchInsert(List<BCustomerInvoicePojo> invoicePojoList) throws Exception {

        if(invoicePojoList==null && invoicePojoList.isEmpty()){
            return SysConstant.INT_ZERO;
        }

        int affectRows = 0;
        for(BCustomerInvoicePojo pojo : invoicePojoList){
            affectRows += SpringAopUtil.getBean(this.getClass()).insert(pojo);
        }
        return affectRows;
    }

    /**
     * 根据客户 id 加载客户对应的发票信息
     * @param customerId
     * @return
     */
    public List<BCustomerInvoicePojo> queryInvoiceByCustomerId(String customerId) {
        if(customerId==null){
            return null;
        }

        return bCustomerInvoiceMapper.queryInvoiceByCustomerId(customerId);
    }

    /**
     * 发票批量处理
     * @param invoicePojoList
     * @param customerId
     * @return
     */
    public int batchUpdate(List<BCustomerInvoicePojo> invoicePojoList, String customerId) throws Exception {

        if(invoicePojoList==null || invoicePojoList.isEmpty() || customerId==null){
            return SysConstant.INT_ZERO;
        }

        // 根据 customerid 检索对应的发票列表
        List<BCustomerInvoicePojo> updateBeforeInvoicePojoList = bCustomerInvoiceMapper.queryInvoiceByCustomerId(customerId);

        // 1.原始列表为空，直接直接插入就好
        if(updateBeforeInvoicePojoList==null || updateBeforeInvoicePojoList.isEmpty()){
            int affectRows = 0;
            for(BCustomerInvoicePojo pojo : invoicePojoList){
                if(DataTypeUtil.isEmptyStr(pojo.getId())){
                    pojo.setId(this.getSysStrId());
                }
                if(DataTypeUtil.isEmptyStr(pojo.getCustomerId())){
                    pojo.setCustomerId(customerId);
                }
                affectRows += SpringAopUtil.getBean(this.getClass()).insert(pojo);
            }
            return affectRows;
        }

        // 2.原始列表不为 null，有数据，则要做对比后再做处理
        // 2.1 找出要更新的
        List<BCustomerInvoicePojo> waitUpdateList = new ArrayList<>();
        for(BCustomerInvoicePojo pojo1 : invoicePojoList){
            for(BCustomerInvoicePojo pojo2 : updateBeforeInvoicePojoList){
                if(pojo1.getId()!=null && pojo2.getId()!=null && pojo1.getId().equals(pojo2.getId())){
                    waitUpdateList.add(pojo1);
                }
            }
        }

        // 2.2 找出要删除的
        List<BCustomerInvoicePojo> waitDeleteList = new ArrayList<>();
        for(BCustomerInvoicePojo pojo2 : updateBeforeInvoicePojoList){
            boolean isExist = false;
            for(BCustomerInvoicePojo pojo1 : invoicePojoList){
                if(pojo1.getId()!=null && pojo2.getId()!=null && pojo1.getId().equals(pojo2.getId())){
                    isExist = true;
                    break;
                }
            }
            if(!isExist){
                waitDeleteList.add(pojo2);
            }
        }

        // 2.3 找出要插入的
        List<BCustomerInvoicePojo> waitInsertList = new ArrayList<>();
        for(BCustomerInvoicePojo pojo1 : invoicePojoList){
            boolean isExist= false;
            for(BCustomerInvoicePojo pojo2 : updateBeforeInvoicePojoList){
                if(pojo1.getId()!=null && pojo2.getId()!=null && pojo1.getId().equals(pojo2.getId())){
                    isExist = true;
                    break;
                }
            }
            if(!isExist){
                waitInsertList.add(pojo1);
            }
        }


        int updateRows = 0;
        int deleteRows = 0;
        int insertRows = 0;
        // 待更新的列表有数据
        if(!waitUpdateList.isEmpty()){
            for(BCustomerInvoicePojo pojo : waitUpdateList){
                updateRows += SpringAopUtil.getBean(this.getClass()).update(pojo);
            }
        }

        if(!waitDeleteList.isEmpty()){
            for(BCustomerInvoicePojo pojo : waitDeleteList){
                deleteRows += SpringAopUtil.getBean(this.getClass()).delete(pojo.getId());
            }
        }

        if(!waitInsertList.isEmpty()){
            for(BCustomerInvoicePojo pojo : waitInsertList){
                insertRows += SpringAopUtil.getBean(this.getClass()).insert(pojo);
            }
        }

        return updateRows + deleteRows + insertRows;

    }
}
